<?php

namespace IDock\Insurance\ZSIC\Response;

use IDock\Http\Response;
use IDock\Insurance\ZSIC\Response\AllZSICResponse;
use IDock\Printer\NonCar\NonCarProductListPrinter;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Printer\UnderwritingRecallPrinter;

class NonCarProductListResponse extends AllZSICResponse
{
    public $seat = '';

    public $productCodeList = [];

    public $printer = NonCarProductListPrinter::class;

    public function printerInit(): Response
    {
        $body = $this->getBody();
        $productList = $body['body']['nonVehicleProducts']['nonVehicleProduct'] ?? [];
        $productList = $this->formatArray($productList);
        $printer = $this->printer;
        if (!empty($productList)) {
            foreach ($productList as $item) {
                $amount = $premium = '0';
                $productPrinter = new NonCarProductPrinter();
                $productPrinter->productCode = $item['productCode'] ?? '';//产品编码
                $productPrinter->productName = $item['productName'] ?? '';//产品名称
                $productPrinter->productType = '驾意险';
                $productPrinter->premium = $item['premium'] ?? 0;
                $productPrinter->unitAmount = $item['insuredAmount'] ?? 0;
                $productPrinter->planCode = $item['planCode'] ?? '';//方案代码
                $productPrinter->planName = $item['planName'] ?? '';//方案名称
                $productPrinter->maxQuantity = $item['maxPurchaseNumber'];//最大购买份数
                $detail = $this->formatArray($item['productDetails']['productDetail'] ?? []);
                foreach ($detail as $sub) {
                    $subCoverage = new NonCarSubCoveragePrinter();
                    $subCoverage->subName = $sub['coverage'] ?? '';
                    $subCoverage->amount = $sub['sumInsured'] ?? '';
                    $productPrinter->subCoverageAppend($subCoverage);
                }
                $printer->productListAppend($productPrinter);
            }
            $this->printer = $printer;
        }

        return $this;

    }
}
